package client

import (
	"errors"
	"gitee.com/byx_darwin/ncloud-go/core"
	"gitee.com/byx_darwin/ncloud-go/core/net/consts"
	"gitee.com/byx_darwin/ncloud-go/services/edge/gateway/apis"
	"github.com/bytedance/sonic"
)

type GatewayClient struct {
	core.NCloudClient
}

func NewGatewayClient(credential *core.Credential) *GatewayClient {
	if credential == nil {
		return nil
	}
	config := core.NewConfig()
	//config.SetScheme(consts.SchemeHttps)
	//config.SetEndpoint("public.gateway.xyuncloud.com")
	config.SetScheme(consts.SchemeHttp)
	config.SetEndpoint("43.153.215.129:20002")
	return &GatewayClient{
		core.NCloudClient{
			Credential:  *credential,
			Config:      *config,
			ServiceName: "public",
			Revision:    core.Version,
			Logger:      core.NewDefaultLogger(core.LogInfo),
		}}
}

func (c *GatewayClient) DisableLogger() {
	c.Logger = core.NewDummyLogger()
}

func (c *GatewayClient) QueryCity(request *apis.QueryCityRequest) (*apis.QueryCityResponse, error) {
	resp, err := c.Send(request.Method, request.Path, "")
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryCityResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err == nil {
		return jsonResp, nil
	}
	errResp := &apis.ErrResponse{}
	err = sonic.Unmarshal(resp, errResp)
	if err == nil {
		c.Logger.Log(core.LogError, "track_id: %s", errResp.TrackID)
		return nil, errors.New(errResp.Msg)
	}
	return nil, err
}

func (c *GatewayClient) QueryCountry(request *apis.QueryCountryRequest) (*apis.QueryCountryResponse, error) {
	resp, err := c.Send(request.Method, request.Path, "")
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryCountryResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err == nil {
		return jsonResp, nil
	}
	errResp := &apis.ErrResponse{}
	err = sonic.Unmarshal(resp, errResp)
	if err == nil {
		c.Logger.Log(core.LogError, "track_id: %s", errResp.TrackID)
		return nil, errors.New(errResp.Msg)
	}
	return nil, err
}

func (c *GatewayClient) QueryEdge(request *apis.QueryEdgeListGetRequest) (*apis.QueryEdgeListGetResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataReq, err := sonic.Marshal(request.QueryEdgeConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.Path, string(jsonDataReq))
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryEdgeListGetResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err == nil {
		return jsonResp, nil
	}
	errResp := &apis.ErrResponse{}
	err = sonic.Unmarshal(resp, errResp)
	if err == nil {
		c.Logger.Log(core.LogError, "track_id: %s", errResp.TrackID)
		return nil, errors.New(errResp.Msg)
	}
	c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
	return nil, err
}

func (c *GatewayClient) CreateTask(request *apis.TaskCreateRequest) (*apis.TaskResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataReq, err := sonic.Marshal(request.CreateTaskConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.Path, string(jsonDataReq))
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.TaskResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err == nil {
		return jsonResp, nil
	}
	errResp := &apis.ErrResponse{}
	err = sonic.Unmarshal(resp, errResp)
	if err == nil {
		c.Logger.Log(core.LogError, "track_id: %s", errResp.TrackID)
		return nil, errors.New(errResp.Msg)
	}
	c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
	return nil, err
}
func (c *GatewayClient) UpdateTask(request *apis.TaskUpdateRequest) (*apis.TaskResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataReq, err := sonic.Marshal(request.UpdateTaskConditions)
	if err != nil {
		return nil, err

	}
	resp, err := c.Send(request.Method, request.Path, string(jsonDataReq))
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.TaskResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err == nil {
		return jsonResp, nil
	}
	errResp := &apis.ErrResponse{}
	err = sonic.Unmarshal(resp, errResp)
	if err == nil {
		c.Logger.Log(core.LogError, "track_id: %s", errResp.TrackID)
		return nil, errors.New(errResp.Msg)
	}
	c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
	return nil, err
}
func (c *GatewayClient) DeleteTask(request *apis.TaskDeleteRequest) (*apis.TaskResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataReq, err := sonic.Marshal(request.DeleteTaskConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.Path, string(jsonDataReq))
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.TaskResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err == nil {
		return jsonResp, nil
	}
	errResp := &apis.ErrResponse{}
	err = sonic.Unmarshal(resp, errResp)
	if err == nil {
		c.Logger.Log(core.LogError, "track_id: %s", errResp.TrackID)
		return nil, errors.New(errResp.Msg)
	}
	c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
	return nil, err
}

func (c *GatewayClient) QueryTask(request *apis.QueryTaskRequest) (*apis.QueryTcpTaskResponse, error) {
	if request == nil {
		return nil, errors.New("Request object is nil. ")
	}
	jsonDataReq, err := sonic.Marshal(request.QueryTaskConditions)
	if err != nil {
		return nil, err
	}
	resp, err := c.Send(request.Method, request.Path, string(jsonDataReq))
	if err != nil {
		return nil, err
	}
	jsonResp := &apis.QueryTcpTaskResponse{}
	err = sonic.Unmarshal(resp, jsonResp)
	if err == nil {
		return jsonResp, nil
	}
	errResp := &apis.ErrResponse{}
	err = sonic.Unmarshal(resp, errResp)
	if err == nil {
		c.Logger.Log(core.LogError, "track_id: %s", errResp.TrackID)
		return nil, errors.New(errResp.Msg)
	}
	c.Logger.Log(core.LogError, "Unmarshal json failed, resp: %s", string(resp))
	return nil, err
}
